﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineVideos.ViewModels;
using OnlineVideos.Entities;
using Common.Library;
using System.ComponentModel;
using OnlineVideos.Data;

namespace OnlineVideos
{
    public partial class StoryHistory : UserControl
    {
        History objHistory = new History();
        List<LinkHistory> showHistoryVideos = null;
        string link = string.Empty;
        private bool IsDataLoaded;
        int songLastIndex = 1;
        int songFirstIndex = 1;
        int songsHisCount = 0;
        public StoryHistory()
        {
            InitializeComponent();
            IsDataLoaded = false;
            showHistoryVideos = new List<LinkHistory>();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsDataLoaded == false)
            {
                GetPageDataInBackground();
                IsDataLoaded = true;
            }

        }

        private void GetPageDataInBackground()
        {
            songLastIndex = objHistory.GetLastHisId(Constants.SongHistoryFile);
            songFirstIndex = songFirstIndex - 14;
            if (songFirstIndex < 0)
                songFirstIndex = 1;
            songsHisCount = 15;


            BackgroundHelper bwHelper = new BackgroundHelper();
            bwHelper.AddBackgroundTask(
                                               (object s, DoWorkEventArgs a) =>
                                               {
                                                   a.Result = objHistory.LoadSongHistory(songFirstIndex, songLastIndex, songsHisCount);
                                               },
                                               (object s, RunWorkerCompletedEventArgs a) =>
                                               {


                                                   showHistoryVideos = (List<LinkHistory>)a.Result;
                                                   if (showHistoryVideos.Count > 0)
                                                   {
                                                       lbxSongHistory.ItemsSource = showHistoryVideos;
                                                       tblkHistoryNoSongs.Visibility = Visibility.Collapsed;
                                                   }
                                                   else
                                                   {
                                                       tblkHistoryNoSongs.Text = "No " + AppResources.FavoriteSongsPivotTitle + " available";
                                                       tblkHistoryNoSongs.Visibility = Visibility.Visible;
                                                   }
                                               }
                                             );
            bwHelper.RunBackgroundWorkers();
        }

        private void lbxSongHistory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxSongHistory.SelectedIndex == -1)
                    return;
                YoutubeApi.Youtube.PlayYoutubeVideo((lbxSongHistory.SelectedItem as LinkHistory).LinkUrl);
               
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxSongHistory_SelectionChanged Method In HistoryPivot file.", ex);
            }
            lbxSongHistory.SelectedIndex = -1;
        }
    }
}
